
var G = new Object();
G.res = "/resources/";

/*
 * example : 
 *
 *   G.ajax({
 *      url         : "a.ajax",
 *      type        : "POST",
 *      data        : "a=1&b=2",
 *      OnOK        : function(txt) { ... },
 *      OnErr       : function(txt) { ... },
 *      OnStart     : function() { ... },
 *      OnComplete  : function(obj) { ... }
 *   });
 */
G.ajax = function (options) {
    var OnOK = options.OnOK ? options.OnOK : function() {};
    var OnErr = options.OnErr ? options.OnErr : function() {};
    var OnStart = options.OnStart ? options.OnStart : function() {};
    var OnComplete = options.OnComplete ? options.OnComplete : function() {};

    var type = options.type ? options.type : "POST";
    var url = options.url;
    var data = options.data;
    var cache = false;
    var dataType = 'json';
    
    var error = function(txt, sta) { OnErr(sta); }
    var complete = function(obj, sta) { OnComplete(obj) };
    var success = function(mix, sta) {
        if (mix.flag == 1) {            // AJAX_CONST_OK
            OnOK(mix.result);
        } else if (mix.flag == 2) {     // AJAX_CONST_ERROR
            OnErr(mix.result);
        } else if (mix.flag == 3) {     // AJAX_CONST_REDIRECT
            top.location = mix.result;
        } else {
            OnErr(mix.result);
        }
    };

    OnStart();
    $.ajax({
        url         : url,
        type        : type,
        data        : data,
        cache       : cache,
        dataType    : dataType,
        success     : success,
        error       : error,
        complete    : complete
    });
}

/*
 *  require jquery.urlEncode
 * */
G.dataEncode = function (data_arr) {
    var rlt = [];
    $.each(data_arr, function (key, value) {
            rlt.push(key + "=" + encodeURIComponent(value));
        });
    return rlt.join("&");

}

G.err = function (msg) {
    alert("错误\n\n"+msg);
}

G.info = function (msg) {
    alert("消息\n\n"+msg);
}

